package algorithm.slidingwindow;

import org.junit.Test;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2/18/2022 3:35 PM
 */
public class SlidingWindowDemo {

    @Test
    public void test() {
        int[] arr = {2, 3, 1, 2, 4, 3};
        minSubArrayLen(7, arr);
    }

    public void minSubArrayLen(int target, int[] nums) {
        int length = nums.length;
        int index = Integer.MAX_VALUE;
        int sum = 0;
        int start = 0;
        int end = 0;
        while (end < length) {
            sum += nums[end];
            while (sum >= target) {
                // 窗口内的总和 减去 start 位置的值
                sum -= nums[start];
                index = Math.min(index, end - start + 1);
                start++;
            }
            end++;
        }
    }
}
